package com.xianyv.exception;

import com.xianyv.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Result constraintExceptionHandler(SQLIntegrityConstraintViolationException e){
        log.error(e.getMessage() + "--->这是SQLIntegrityConstraintViolationException.class");
        String message = e.getMessage();
        if (message.contains("Duplicate entry")){
            message = "该工号已存在";
        }

        return Result.error(message);
    }

    @ExceptionHandler(SQLException.class)
    public Result SQLExceptionHandler(SQLException e){
        log.error(e.getMessage() + "--->SQLException.class");
        return Result.error(e.getMessage());
    }
}
